﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace Presentation
{
    public partial class frm_ChuyenBay : Form
    {
        private ChuyenBayBUS cbBUS = new ChuyenBayBUS();
        private ChuyenBayDTO cbDTO = new ChuyenBayDTO(); 

        public frm_ChuyenBay()
        {
            InitializeComponent();
        }
        public void HienThiDSChuyenBay()
        {
            lvChuyenBay.FullRowSelect = true;
            lvChuyenBay.Items.Clear();

            int i;
            string str;
            DataTable dt;

            dt = cbBUS.LayDanhSach();
            for (i = 0; i < dt.Rows.Count; i++)
            {
                ListViewItem lvi;
                lvi = lvChuyenBay.Items.Add((i + 1).ToString());
                str = dt.Rows[i]["MACHUYENBAY"].ToString();
                lvi.SubItems.Add(str);
                str = dt.Rows[i]["MATUYENBAY"].ToString();
                lvi.SubItems.Add(str);
                str = dt.Rows[i]["NGAYGIO"].ToString();
                lvi.SubItems.Add(str);
                str = dt.Rows[i]["KHOIHANH"].ToString();
                lvi.SubItems.Add(str);
                str = dt.Rows[i]["DEN"].ToString();
                lvi.SubItems.Add(str);
                str = dt.Rows[i]["THOIGIANBAY"].ToString();
                lvi.SubItems.Add(str);
                str = dt.Rows[i]["SLGHEHANG1"].ToString();
                lvi.SubItems.Add(str);
                str = dt.Rows[i]["SLGHEHANG2"].ToString();
                lvi.SubItems.Add(str);



            }

        }
        

        private void frm_ChuyenBay_Load(object sender, EventArgs e)
        {
            HienThiDSChuyenBay(); 
        }
        private void SoanLai()
        {
            txtMaCB.Text = "";
            txtMaTB.Text = "";
            txthang1.Text = "";
            txthang2.Text = "";
        }

        private void btnSoanLai_Click(object sender, EventArgs e)
        {
            SoanLai();
        }

        private void btnTroVe_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaCB.Text != "")
            {
                string str;
                DateTime ngio,kh,nden;
               // int  gh1, gh2;
                bool ktlap = true;
                str = txtMaCB.Text;
                for (int i = 0; i < lvChuyenBay.Items.Count; i++)
                {
                    if (str.Trim().Equals(lvChuyenBay.Items[i].SubItems[1].Text.ToString().Trim()))
                    {
                        ktlap = false; break;
                    }
                    else
                        ktlap = true;
                }

                //thêm chuyen bay
                if (ktlap == true)
                {
                    cbDTO.Machuyenbay= "'"+txtMaCB.Text+"'";
                    cbDTO.Matuyenbay = txtMaTB.Text;
                    string ngaygio = this.dtNgaygio.Text;
                    ngio= Convert.ToDateTime(ngaygio.ToString());

                    string khoihanh = this.dtKhoihanh.Text;
                    kh = Convert.ToDateTime(khoihanh.ToString());

                    string den = this.dtDen.Text;
                    nden = Convert.ToDateTime(den.ToString());
                                                          
                    cbDTO.Ngaygio =ngio;
                    cbDTO.Khoihanh =kh;
                    cbDTO.Den = nden;

                    cbDTO.Ghehang1 = Convert.ToInt32(txthang1.Text);
                    cbDTO.Ghehang2 = Convert.ToInt32(txthang2.Text);
                                                           
                    cbBUS.Them(cbDTO);
                    HienThiDSChuyenBay();

                    MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   SoanLai();
                }
                else
                    MessageBox.Show("Chuyen bay da co trong danh sach", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            else
            {
                MessageBox.Show("Ban chua nhap ma chuyen bay", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (lvChuyenBay.SelectedIndices.Count > 0)
            {
                string str = "";
                str = lvChuyenBay.Items[lvChuyenBay.SelectedIndices[0]].SubItems[1].Text;
                if (MessageBox.Show("Bạn có chắc chắn là muốn xóa chuyen bay '" + str + "' không ?", "Hỏi lại", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    cbBUS.Xoa(lvChuyenBay.SelectedIndices[0]);
                    lvChuyenBay.Items.Remove(lvChuyenBay.SelectedItems[0]);
                    MessageBox.Show("Chuyen bay '" + txtMaCB.Text + "'da duoc xoa.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            SoanLai();
          
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {

        }

      
    }
}
